﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using FrienDev.Data;
using FrienDev.BusinessRules;
using System.Data;
using System.Web.Script.Services;

/// <summary>
/// Summary description for SelectFriendsService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
[ScriptService]
public class SelectFriendsService : System.Web.Services.WebService
{

    public SelectFriendsService()
    {

        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }

    [WebMethod(EnableSession=true)]
    public List<UserItem> GetNameListAsJson(string InputText)
    {

        string trueText = System.Web.HttpUtility.UrlDecode(InputText);
        List<User> m_OriList = FriendRules.Instance.GetCurrentUserFriends();
        List<UserItem> m_SourceList = new List<UserItem>();
        for (int a = 0; a < m_OriList.Count; a++)
        {
            m_SourceList.Add(new UserItem(m_OriList[a].UserId, m_OriList[a].FullName, m_OriList[a].PhotoFileName));

        }
        List<UserItem> m_TargetList = new List<UserItem>();
        for (int k = m_SourceList.Count - 1; k >= 0; k--)
        {
            if (m_SourceList[k].FullName.Contains(trueText))
            {
                m_TargetList.Add(m_SourceList[k]);
                m_SourceList.Remove(m_SourceList[k]);

            }
            else
            {
                if (k <= 0)
                {
                    break;
                }
                continue;
            }
        }

        for (int i = m_SourceList.Count - 1; i >= 0; i--)
        {
            string temp = ConvertHzToPz_Gb2312.Convert(m_SourceList[i].FullName);
            if (temp.Contains(trueText.ToLower()))
            {
                m_TargetList.Add(m_SourceList[i]);
                m_SourceList.Remove(m_SourceList[i]);

            }
            else
            {
                if (i <= 0)
                {
                    break;
                }
                continue;

            }
        }

        for (int j = m_SourceList.Count - 1; j >= 0; j--)
        {
            string tp = StrToPinyin.GetChineseSpell(m_SourceList[j].FullName);
            if (tp.Contains(trueText.ToUpper()))
            {
                m_TargetList.Add(m_SourceList[j]);
                m_SourceList.Remove(m_SourceList[j]);
            }
            else
            {
                if (j <= 0)
                {
                    break;
                }
                continue;
            }
        }

        return m_TargetList;

    }

    [WebMethod]
    public void SendMessage(string[] Receivers, string sendContent)
    {
        MessageRules.Instance.SendMessage(Receivers, sendContent);
    }
}

public class UserItem
{
    public UserItem()
    { }

    public UserItem(string UserId, string FullName, string PhotoFileName)
    {
        this.UserId = UserId;
        this.FullName = FullName;
        this.PhotoFileName = PhotoFileName;
    }

    private string m_UserId;
    public string UserId
    {
        get
        {
            return m_UserId;
        }
        set
        {
            m_UserId = value;
        }
    }

    private string m_FullName;
    public string FullName
    {
        get
        {
            return m_FullName;
        }
        set
        {
            m_FullName = value;
        }
    }

    private string m_PhotoFileName;
    public string PhotoFileName
    {
        get
        {
            return m_PhotoFileName;
        }
        set
        {
            m_PhotoFileName = value;
        }
    }

}

